<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-button
        class="filter-item"
        v-permission="'Article::createCategory'"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="tableData.data"
      size="small"
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" prop="id" sortable />
      <el-table-column align="center" label="分类名称" prop="name" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.status ? '正常' : '禁用' }}</span>
        </template>
      </el-table-column>
      <!-- 操作按钮组件 -->
      <action-button
        :handle-update="handleUpdate"
        :handle-delete="handleDelete"
        :loading="deleteLoading"
        update-auth="Article::updateCategory"
        delete-auth="Article::deleteCategory"
      />
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="tableData.total > 0"
      :total="tableData.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <dialog-form
      ref="dataForm"
      :status="dialogStatus"
      :rules="rules"
      :data="dataForm"
      :loading="dialogFormLoading"
      @createData="createData"
      @updateData="updateData"
      width="500px"
    >
      <template>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="dataForm.name" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch v-model="dataForm.status" active-text="正常" inactive-text="禁用"></el-switch>
        </el-form-item>
      </template>
    </dialog-form>
  </div>
</template>

<script>
import { listCategory, createCategory, updateCategory, deleteCategory } from '@/api/article'
import table from '@/mixins/table'

export default {
  name: 'Category',
  mixins: [table],
  data() {
    return {
      pageTitle: '文章分类',
      rules: {
        name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }]
      },
      roles: [],
      getListApi: listCategory,
      createApi: createCategory,
      updateApi: updateCategory,
      deleteApi: deleteCategory
    }
  },
  created() {},
  methods: {
    resetForm() {
      this.dataForm = {
        id: undefined,
        name: undefined,
        status: true
      }
    },
    afterCreate() {
      // 创建成功后返回操作，根据自定义覆盖，但大部分时候你不需要覆盖它
      this.getList()
    },
    afterUpdate() {
      // 更新成功操作， 根据自定义覆盖 但大部分情况你不需要覆盖它
      this.getList()
    },
    afterDelete(index) {
      // 删除后处理，大部分时候你不需要覆盖它
      this.getList()
    }
  }
}
</script>
